/**
 * FavoriteBooks.java
 *
 * Copyright (c) 2011 Alexandr Tsvetkov (E-mail: al@ukr.net)
 *
 * License agreement:
 *
 * 1. This code is published AS IS. Author is not responsible for any damage that can be
 *    caused by any application that uses this code.
 * 2. Author does not give a garantee, that this code is error free.
 * 3. This code can be used in NON-COMMERCIAL applications AS IS without any special
 *    permission from author.
 * 4. This code can be modified without any special permission from author IF AND ONLY IF
 *    this license agreement will remain unchanged.
 * 5. SPECIAL PERMISSION for this code usage in COMMERCIAL application SHOULD be obtained
 *    from author.
 */
package ua.at.tsvetkov.samizdat.data;

import ua.at.tsvetkov.data.StoreString;
import ua.at.tsvetkov.ui.Rout;
import ua.at.tsvetkov.util.Log;

import android.app.Activity;
import android.widget.Toast;

import ua.at.tsvetkov.samizdat.Const;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class FavoriteBooks extends StoreString {

	private Activity	context;

	public FavoriteBooks(Activity context) {
		super(Const.FILE_FAVORITE_BOOKS);
		this.context = context;
	}

	public boolean isFavorite(String url) {
		if (isContainsSubstring(url)) {
			return true;
		}
		return false;
	}

	public boolean isFavorite(Book book) {
		if (isContainsSubstring(book.object)) {
			return true;
		}
		return false;
	}

	/**
	 * Внести/убрать из избранных книг
	 */
	public void setFavorite(Book book) {
		if (isContainsSubstring(book.object)) {
			removeWithSubstring(book.object);
			Rout.toast(context, "Книга удалена из избранных", Toast.LENGTH_SHORT);
		} else {
			add(book.getFormattedString());
			Rout.toast(context, "Книга добавлена в избранное", Toast.LENGTH_SHORT);
		}
		save();
	}

	public String[] getBookData() {
		return (String[]) data.toArray(new String[data.size()]);
	}

	public String[] getBookDataFromHeaders() {
		String[] stBooks = new String[data.size()];
		int pos = 0;
		for (String st : data) {
			Book book = new Book();
			try {
				book.initFromHeader(st);
			} catch (WrongFieldCountException e) {
				Log.w("Неправильный формат заголовка", e);
			}
			stBooks[pos++] = book.getFormattedString();
		}
		return stBooks;
	}
}
